import { ref, computed, Transition, reactive, onMounted } from "vue";
import { type PaginationProps } from "@pureadmin/table";
import { ElMessage, FormInstance } from "element-plus";
import {
  SecuritiesPortfolioRiskEarlyWarning,
  SecuritiesPortfolioGetPurchasedPortfolioData,
  SecuritiesPortfolioSendWarehouseTransferPrompt,
} from "@/api/stu/Cecurity";
import { useRoute } from "vue-router";
export function usePortfolioRebalancing() {
  const route = useRoute()
  const dataList = ref();
  const CreditFrom = ref();
  const loading = ref<boolean>(true);
  const btnloading = ref<boolean>(false);
  const DialogVisible = ref<boolean>(false);
  const SERiskEarly = ref<string>("");
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    pageSizes: [5, 10, 15],
    background: true
  });
  const RiskEarly = [
    { label: "收益率低于预期", value: "1" },
    { label: "波动率高于预期", value: "2" },
  ]
  const columns: TableColumnList = [
    {
      label: "序号",
      type: "index",
      minWidth: 100
    }, {
      label: "投资组合名称",
      prop: "portfolioldName"
    }, {
      label: "客群名称",
      prop: "clientBaseName",
    }, {
      label: "模拟年化收益率(%)",
      prop: "annualYield"
    },
    // {
    //   label: "持仓收益率(%)",
    //   prop: "positionYield"
    // },
    {
      label: "持仓收益率(%)",
      prop: "positionYieldByCompany"
    },
    {
      label: "模拟年化波动率(%)",
      prop: "annualizedVolatility"
    }, {
      label: "持仓波动率(%)",
      prop: "positionVolatility"
    },
    {
      label: "操作",
      fixed: "right",
      width: 180,
      slot: "operation"
    }
  ];
  const BtnLoading = ref<boolean>(false)
  // 调仓提示-风险预警
  async function TransferTip(row, num) {
    if (num) {
      const data = {
        "planId": route.query.planId,
        "taskId": route.query.taskId,
        portfolioId: row.portfolioldId,
        warningType: 1
      };
      btnloading.value = true;
      const res = await SecuritiesPortfolioSendWarehouseTransferPrompt(data);
      if (res.success) {
        btnloading.value = false;
        ElMessage.success(res.msg);
        GetDataList()
      } else {
        ElMessage.error(res.msg);
        setTimeout(() => {
          btnloading.value = false;
        }, 1500)
      }
    } else {
      CreditFrom.value = row;
      DialogVisible.value = true;
    }
  }

  //  提交风险预警
  async function stubim_Visible() {
    if (!SERiskEarly.value) {
      ElMessage.warning("请选择风险预警")
      return
    }
    const data = {
      "planId": route.query.planId,
      "taskId": route.query.taskId,
      portfolioId: CreditFrom.value.portfolioldId,
      warningType: SERiskEarly.value
    };
    BtnLoading.value = true;
    const res = await SecuritiesPortfolioRiskEarlyWarning(data);
    // console.log("风险预警", res)
    if (res.success) {
      ElMessage.success(res.msg);
      BtnLoading.value = false;
      GetDataList()
      close_Visible()
    } else {
      setTimeout(() => { BtnLoading.value = false; }, 1500)
      ElMessage.error(res.msg);
    }
  }

  //  关闭风险预警
  function close_Visible() {
    DialogVisible.value = false;
    CreditFrom.value = "";
    SERiskEarly.value = null;
  }

  function handleSizeChange() { GetDataList() }
  function handleCurrentChange() { GetDataList() }
  async function GetDataList() {
    const data = {
      pageIndex: pagination.currentPage,
      pageCount: pagination.pageSize,
      taskId: route.query.taskId,
      planId: route.query.planId,
    };
    const res = await SecuritiesPortfolioGetPurchasedPortfolioData(data);
    if (res.success) {
      dataList.value = res.data.items;
      pagination.total = res.data.totalCount;
      loading.value = false;
    } else {
      ElMessage.error(res.msg);
      setTimeout(() => { loading.value = false; }, 1500)
    }
  }

  onMounted(() => {
    GetDataList()
  })

  return {
    columns,
    loading,
    dataList,
    BtnLoading,
    CreditFrom,
    RiskEarly,
    btnloading,
    SERiskEarly,
    pagination,
    DialogVisible,
    stubim_Visible,
    close_Visible,
    TransferTip,
    handleSizeChange,
    handleCurrentChange
  }
}
